<?php
namespace app\wx\controller;
use think\facade\Request;
use app\wx\validate\DepartmentValidate;
use app\wx\service\DepartmentService;
use think\facade\Config;

class Department
{
    //查询
    public function findDepartment()
    {
        $page = Request::post('page/d', 1); //当前页
        $limit = Request::post('limit/d', 10); //每页数量
        $field = Request::post('field/s', "*"); //寻找字段
        //接收参数post,查询条件待定
        $param = array(
            'id'      => Request::post('id/d',''), //id
            'name'    => Request::post('name/s',''), //部门名称
            'name_en' => Request::post('name_en/s',''), // 英文名称
            'start_time' => Request::post('startTime/s', ''), //开始时间
            'end_time' => Request::post('entTime/s', '') //结束时间
        );
        $data = DepartmentService::findDepartment($param,$page,$limit,$field);
        return success($data);
    }

    //添加接口
    public function add()
    {
        //接受参数
        $param = array(
            'name'     => Request::post('name/s',''),
            'name_en'  => Request::post('name_en/s',''),
            'parentid' => Request::post('name_en/d',0),
            'order'    => Request::post('order/d',0),
        );
        $param['think_type'] = 'add';
        $validate = new DepartmentValidate();
        if (!$validate->scene('add')->check($param)) {
            error($validate->scene('add')->getError());
        }
        unset($param['think_type']);
        $id = DepartmentService::add($param);
        return success([
            'id' => $id
        ]);
    }

    //修改接口
    public function edit()
    {
        //接受参数
        $param = array(
            'id'       => Request::post('id/d',0),
            'name'     => Request::post('name/s',''),
            'name_en'  => Request::post('name_en/s',''),
            'parentid' => Request::post('name_en/d',0),
            'order'    => Request::post('order/d',0),
        );
        $param['think_type'] = 'edit';
        $validate = new DepartmentValidate();
        if (!$validate->scene('edit')->check($param)) {
            error($validate->scene('edit')->getError());
        }
        unset($param['think_type']);
        DepartmentService::edit($param);
        return success();
    }

    //微信同步本地接口
    public function asyncDepartment()
    {
        set_time_limit(0);
        //获取微信token
        $token_url = Config::get('wx.get_token_url');
        $corpid = Config::get('wx.corpid');
        $corpsecret = Config::get('wx.corpsecret');
        $result = http_get($token_url.'?corpid='.$corpid.'&corpsecret='.$corpsecret);
        $result = json_decode($result, true);
        if($result['errcode'] !== 0)
            error($result['errmsg']);
        //获取部门
        $department_url = Config::get('wx.get_department_url');
        $department_result = http_get($department_url.'?access_token='.$result['access_token']);
        $department_result = json_decode($department_result, true);
        if($department_result['errcode'] !== 0)
            error($department_result['errmsg']);
        if(!empty($department_result['department']))
        {
            DepartmentService::insertAll($department_result['department']);
        }
        return success([],'同步成功');
    }
}
